from tkinter import *
from openpyxl import Workbook
from openpyxl import load_workbook
import os

root = Tk()

if os.path.exists("scores.xlsx"):
  wb = load_workbook("scores.xlsx")
else:
  wb = Workbook()

ws = wb.active
ws['A1'] = '姓名'
ws['B1'] = '语文'
ws['C1'] = '数学'
ws['D1'] = '英语'
wb.save("scores.xlsx")

frame = Frame(root)
frame.pack(padx=10, pady=10)

Label(frame, text="姓名").grid(row=0)
Label(frame, text="语文").grid(row=1)
Label(frame, text="数学").grid(row=2)
Label(frame, text="英语").grid(row=3)

e1 = Entry(frame)
e2 = Entry(frame)
e3 = Entry(frame)
e4 = Entry(frame)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)

def insert():
  name = e1.get()
  cScore = e2.get()
  mCore = e3.get()
  eCore = e4.get()

  row = ws.max_row + 1
  ws.cell(row, 1, value = name)
  ws.cell(row, 2, value = cScore)
  ws.cell(row, 3, value = mCore)
  ws.cell(row, 4, value = eCore)

  wb.save('scores.xlsx')

Button(frame, text="提交", command=insert).grid(row=4, column=1)

root.mainloop()
